layui.use(['form','layer','table','laytpl','laydate'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        laydate = layui.laydate,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
      //  url : '../../json/userList.json',
        url : '../../../order-info-w-m/page',
        cellMinWidth : 10,
        page : true,
        where:{
            orderType:1   // 1 周计划  2 月计划
        },
        height : "full-125",
        limits : [10,15,20,25],
        limit : 15,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'projectName', title: '项目名称', Width:10,align:"center"},
            {field: 'buildingName', title: '栋号', Width:10, align:"center"},
            {field: 'floors', title: '楼层', Width:50, align:"center"},

           {field: 'productName', title: '构件名称', Width:30, align:"center"},
           {field: 'trainNo', title: '车次', Width:30, align:"center"},
          //{field: 'createTime', title: '创建时间', minWidth:100, align:"center"},
          {field: 'startDate', title: '要货开始日期', minWidth:100, align:"center"},
          {field: 'endDate', title: '要货结束日期', minWidth:100, align:"center"},

            // key
          //  {title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });


    //执行一个laydate实例
    laydate.render({
        elem: '#startDate' //指定元素
    });
    //执行一个laydate实例
    laydate.render({
        elem: '#endDate' //指定元素
    });
    /**
     *  u.user_name applyUser,uuu.user_name auditUser,
     p.`name` projectName,p.building,
     w.id workOrderId,w.`name` orderName,w.floor ,w.tower_crane towerCrane,w.remark,w.state,w.order_type orderType
     */
    var projectName = "";
    var state;



    // 项目列表
    var htmls = '<option value="">请选择</option>'; //全局变量
    $.ajax({
        url: '../../project/getall',
        type: "get",
        dataType : "json",
        contentType : "application/json",
        async: false,//这得注意是同步
        success: function (result) {
            resultData = result.data;
            for(var x in resultData){
                htmls += '<option value = "' + resultData[x].projectId + '">' + resultData[x].projectName + '</option>'
            }
            $("#projects").html(htmls);
        }
    });
    //  $("#projects").val(    $("#projectId").val()  );
    form.render('select');//需要渲染一下


    // 导出Excel
    // 获取参数
    function getParams() {
        var startDate = $("#startDate").val();
        var endDate = $("#endDate").val();
        var projectId = $("#projects").val();
        var queryParam = {
            orderType:1,
            startDate:startDate,
            endDate:endDate,
            projectId:projectId

        };
        return queryParam;
    }
    $(".export_btn").click(function () {

        // 导出
        var form = $('<form></form>');
        // 设置属性
        form.attr('action', '../../order-info-w-m/export-wm');//  请求后台的URL
        form.attr('method', 'POST');
        form.attr('target', '_blank');
        form.css('display', 'none');
        var param = getParams();
        if (param) {
            for (var i in param) {
                var input = $('<input type="text" name="' + i + '" value="' + param[i] + '" />');
                form.append(input);
            }
        }
        form.appendTo("body");
        // 提交表单
        form.submit();
        //删除表单
        form.remove();

    })

    //搜索 功能
    $(".search_btn").on("click", function () {

        var startDate = $("#startDate").val();
        var endDate = $("#endDate").val();
        var projectId = $("#projects").val();
        debugger;

        table.reload("userListTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                  //搜索的 关键字  姓名,手机号,账号
                startDate:startDate,
                endDate:endDate,
                projectId:projectId

            }
        })

    });

    //添加
    function addUser(edit){
        var index = layui.layer.open({
            title : "工单详情",
            type : 2,
            content : "workorderInfo.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){

                    /**
                     * 项目名称
                     栋号
                     楼层
                     塔吊
                     工单申请人
                     工单审核人
                     工单状态
                     */
                    body.find("#workOrderId").val(edit.workOrderId);  //设备名称
                    // body.find("#facilityType").val(edit.type);  //设备名称

                    body.find("#projectName").val(edit.projectName);  //
                    body.find("#buildingName").val(edit.buildingName);  //
                    body.find("#floor").val(edit.floor);  //
                    body.find("#towerCrane").val(edit.towerCrane);  //
                    body.find("#remark").val(edit.remark);  //
                    body.find("#startTime").val(edit.startTime);  //
                    body.find("#endTime").val(edit.endTime);  //
                    body.find("#applyUser").val(edit.applyUser);  //
                    body.find("#auditUser").val(edit.auditUser);  //
                    body.find("#auditOpinion").val(edit.auditOpinion);  // confirmTime
                    body.find("#confirmTime").val(edit.confirmTime);  // confirmTime
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    $(".addNews_btn").click(function(){
        addUser();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            ids = [];
        debugger;
        var orderIds;
        if(data.length > 0) {
            for (var i in data) {
                ids.push(data[i].orderInfoWMId);
            }
            orderIds = ids.join(",");
            layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                $.post("../../order-info-w-m/delete-batch",{
                    ids : orderIds  //将需要删除的 ids作为参数传入
                },function(data){
                    top.layer.msg(data.msg);
                    tableIns.reload();
                    layer.close(index);
                })
            })
        }else{
            layer.msg("请选择需要删除的数据");
        }
    })

    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addUser(data);
        }else   if(layEvent === 'del'){ //删除
            // layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
            //     $.post("../../workorder/delete-order",{
            //         workOrderId : data.workOrderId  //将需要删除的newsId作为参数传入
            //     },function(data){
            //         top.layer.msg(data.msg);
            //         tableIns.reload();
            //         layer.close(index);
            //     })
            // });
        }
    });

})
